<template>
  <div class="app-container page-wood-allocating-record">
    <FilterForm :data="filterData" @search="getList" labelWidth="86px" />
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['allocating:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['allocating:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
         <el-button
          type="warning"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleCheckOrder"
          v-hasPermi="['allocating:order:edit']"
        >审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="single"
          @click="handleOpenCheckDialog"
          v-hasPermi="['allocating:order:edit']"
        >反审核</el-button>
      </el-col>
    </el-row>

    <Table
        :api="$Api.sales.allocating.list"
        :selection="true"
        @select="handleSelectionChange"
        @dbClick="rowDblclick"
        ref="Table"
        height="60vh"
        >
      <el-table-column label="操作" align="center" width="130"   fixed="left">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click.native.stop="handleUpdate(scope.row)"
            v-hasPermi="['woodPurchase:purchaseOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click.native.stop="handleDelete(scope.row)"
            v-hasPermi="['woodPurchase:purchaseOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="单据日期" align="center" prop="billTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调货单号" align="center" prop="allocatingId" width="140"/>
      <el-table-column label="供应商" align="center" prop="supplierName" width="120"/>
      <el-table-column label="客户" align="center" prop="customerName" width="120"/>
      <el-table-column label="方数" align="center" prop="salesCount" />
      <el-table-column label="采购金额" align="center" prop="purchaseMoney" />
      <el-table-column label="销售金额" align="center" prop="salesMoney" />
      <el-table-column label="车号" align="center" prop="carNo" width="120"/>
      <el-table-column label="采购单号" align="center" prop="inId" width="160"/>
      <el-table-column label="销售单号" align="center" prop="deliveryId" width="155"/>
      <el-table-column label="销售员" align="center" prop="salesName" />
      <el-table-column label="制单人" align="center" prop="createBy" />
      <el-table-column label="审核状态" align="center" key="auditStatus" fixed="right">
        <template slot-scope="scope">
         <span class="success" v-if="scope.row.auditStatus == 1">已审核</span>
         <span class="error" v-else>未审核</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditor" fixed="right"/>
      <el-table-column label="备注" align="center" prop="remark" />
    </Table>

    <!-- 调货单反审核 -->
    <div class="check-status-dialog">
      <el-dialog
        :visible.sync="open"
        :before-close="tableClose"
        :modal-append-to-body="false"
        :append-to-body="true"
        :close-on-click-modal="false"
        width="65%"
        class="add-dialog"
        >
        <div style="height: 14vh;" class="step-content">
          <el-steps :active="active" finish-status="success" align-center>
            <el-step title="销售出库单" description="删除销售出库单"></el-step>
            <el-step title="采购入库单" description="删除采购入库单"></el-step>
            <el-step title="完成" description="反审核完毕"></el-step>
          </el-steps>
        </div>
        <div class="check-data-list">
        <Table
            ref="tableForm"
            :list="woodSalesDeliveList"
            :loading="loading"
            height="30vh"
            >
            <el-table-column label="销售编号" align="center" prop="deliveryId">
              <template slot-scope="scope">
                <span @click="salesDeliveryClick(scope.row)">{{ scope.row.deliveryId}}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="100">
              <template slot-scope="scope">
                    <div class="btn-group">
                      <div class="btn-del">
                            <el-button
                               type="danger"
                               plain
                               size="mini"
                              @click="delSalesId(scope.row)"
                              v-if="btnShow == 1"
                          >删除</el-button>
                          <el-button v-else-if="btnShow == 2" type="success" icon="el-icon-check" size="mini" circle></el-button>
                      </div>
                  </div>
                </template>
            </el-table-column>
            <el-table-column label="提单号" align="center" prop="inId">
            <template slot-scope="scope">
                <span @click="inIdClick(scope.row)">{{ scope.row.inId}}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="160">
              <template slot-scope="scope">
                    <div class="btn-group">
                      <div class="btn-del">
                            <el-button
                              type="danger"
                               size="mini"
                                plain
                              @click="delPurchaseId(scope.row)"
                              :disabled="btnStop"
                               v-if="btnShowOne == 3"
                          >删除</el-button>
                          <el-button v-else-if="btnShowOne == 4" type="success" icon="el-icon-check" size="mini" circle></el-button>
                      </div>
                      <div class="btn-repeal">
                              <el-button
                                  type="danger"
                                  size="mini"
                                  plain
                                  @click="finishCheck()"
                                  :disabled="nextBtnStop"
                                   v-if="btnShowTwo == 5"
                              >完成</el-button>
                              <el-button v-else-if="btnShowTwo == 6" type="success" icon="el-icon-check" size="mini" circle></el-button>
                        </div>
                  </div>
                </template>
            </el-table-column>
        </Table>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>

export default {
  name: "PurchaseOrder",
    components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中状态
      status:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      filterData: [
                {
                    label: "单据日期",
                    type: "period_date",
                    st: "beginBillTime",
                    end: "endBillTime",
                    scale: 2,
                },
                {
                    label: "供应商",
                    name: "supplierName",
                    type: "supplier",
                },
                {
                    label: "客户",
                    name: "customerName",
                    type: "customer",
                },
                { label: "单据编号", name: "allocatingId" },
                {
                    label: "审核状态",
                    name: "auditStatus",
                    type: "select",
                    select:{
                      option:[{
                              value: '1',
                              label: '已审核'
                            }, {
                              value: '0',
                              label: '未审核'
                            }
                          ],
                        selectLabel: "label",
                        selectValue: "value",
                        multiple:true
                    },
                },
            ],
      btnShow: 1 ,
      btnShowOne: 3,
      btnShowTwo: 5,
      // 按钮禁用
      btnStop: true,
      nextBtnStop: true,
      // 步骤条参数
      active: 0,
      // 审核列表
      woodSalesDeliveList:[
        {
          deliveryId: null,
          inId: null,
          purchaseId:null,
        }
      ],
     
    };
  },
  computed:{

  },
  created() {
    
  },
  methods: {
    /** 查询采购订单信息列表 */
    getList(params) {
            this.$refs.Table.getData(params);
        },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.allocatingId)
      this.status = selection.map(item => item.auditStatus)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
       this.$router.push({
        path: "/sale/receipts/allocating",
      });
    },
    // 双击操作
    rowDblclick(row, column, event) {
      this.handleUpdate(row);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.allocatingId;
      let path = `/allocating/list/${id}`;
      this.$router.push({
        path,
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let state = false;
      let { status } = this;
      for( let i = 0; i < status.length; i++) {
        if( status[i] == 1) {
          state = true;
        }
      }
      if(row.auditStatus == 1 || state == true) {
        this.$message.error('已审核，不能删除');
      }else {
        const allocatingIds = row.allocatingId || this.ids;
        this.$confirm('是否确认删除采购订单信息编号为"' + allocatingIds + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() =>{
            this.$Api.sales.allocating.del(allocatingIds).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          }).catch(() => {});
          })
        }
    },
    // 审核订单 
    handleCheckOrder(){
      let isCheck = false;
      let { ids ,status} = this;
       for( let i = 0; i < status.length; i++) {
        if( status[i] == 0) {
          isCheck = true
        }
      }
      if( isCheck == true ) {
         this.$confirm('是否确认 审核 编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.$Api.sales.allocating.setState({
            ids: ids,
            auditStatus: 1
        }).then( (res) =>{
          if(res.code == 200){
              this.getList();
              this.msgSuccess( "审核成功");
          }
        }).catch(() => {
           this.getList();
        });  
        })
      }
    },
    // 反审核过程
    handleOpenCheckDialog(){
      this.open = true;
      let { ids,woodSalesDeliveList } = this;
      ids = ids.toString()
      this.$Api.sales.allocating.detail(ids).then( (res) =>{
        let { data } = res;
        let salesId = data.deliveryId;
        let inId = data.inId;
        woodSalesDeliveList.forEach( (v) =>{
          v.deliveryId = salesId;
          v.inId = inId;
          v.purchaseId = inId;
        })
      })
    },
    // 删除销售出库单
    delSalesId(row) {
      let deliveryIds = row.deliveryId;
      this.$Api.sales.delivery.del(deliveryIds).then( (res) =>{
        let { code } = res;
        if(code == 200 ){
          this.msgSuccess( "操作成功");
          this.active = 1;
          this.btnShow = 2;
          this.btnStop = false;
        }
      }).catch( (res)=>{
        if( res == 'Error: 该销售出库订单不存在！'){
            this.active = 1;
            this.btnShow = 2;
            this.btnStop = false;
        }
      })
      
    },

    // 删除采购入库单
    delPurchaseId(row){
      const inIds = row.purchaseId;
      this.$Api.sales.allocating.delInId({ladingNum: inIds}).then( (res) =>{
        let { code } = res;
        if(code == 200){
          this.active = 2;
          this.btnShowOne = 4;
          this.nextBtnStop = false;
        }
      }).catch( (res)=>{
        if( res.msg == '该采购入库订单不存在！'){
          this.active = 2;
          this.btnShowOne = 4;
          this.nextBtnStop = false;
        }
      })
      
    },
    // 完成
    finishCheck(){
      let { ids } = this;
      let inId  = ids
      this.$Api.sales.allocating.setState({
            ids: inId,
            auditStatus: 0
        }).then( (res) =>{
          if(res.code == 200){
            this.active = 3;
            this.btnShowTwo = 6;
            this.msgSuccess( "审核成功");
            this.getList();
          }
        }).catch(() => {
        }); 
    },
    tableClose(){
      this.open = false;
      if( this.btnShowTwo == 6){ 
        this.btnShow= 1 ;
        this.btnShowOne= 3;
        this.btnShowTwo= 5;
        this.active = 0;
      }
     
    },
    // 查看销售出库详情
    salesDeliveryClick(row){
      const id = row.deliveryId;
      let path = `/sales/salesDelivery/list/${id}`;
      this.$router.push({
        path,
      });
    },
    inIdClick(row){
      const id = row.inId;
      let path = `/purchase/putInstorage/detail/${id}`;
      this.$router.push({
          path,
      });
    },
  }
};
</script>

<style lang="scss" scoped>
::v-deep.check-data-list{
     .btn-group {
        display: flex;
        align-items: center;
        justify-content: space-evenly;
    } 
  }
  .page-wood-allocating-record {
  
  .success{
    color: #67C23A;
  }
  .error{
    color: #F56C6C;
  }
  .disabled {
    color: #a8a8a8;
  }

  .error {
    color: #fa3e3e;
  }

}
</style>